﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SalesSys.Comm.Utility;
using SalesSys.DAL.Base;
using SalesSys.DAL.Models;

namespace SalesSys.DAL.Models
{

    /// <summary>
    /// 审批
    /// </summary>
    public class SequenceApproval : DbEntityBase<Guid>
    {
        public SequenceApproval()
        {
            Records = new List<ApprovalRecord>();
        }

        /// <summary>
        /// 类型名称
        /// </summary>
        public string TypeName { get; set; }
        #region exe

        /// <summary>
        /// 审批节点
        /// </summary>
        private IList<SequenceApprovalItem> items;
        public IList<SequenceApprovalItem> GetItems()
        {
            return items;
        }
        public void SetItems(IEnumerable<SequenceApprovalItem> list)
        {
              items=new List<SequenceApprovalItem>(list);
        }
        public IDictionary<string, object> GetParams()
        {
            var v = SimpleJson.Deserialize<IDictionary<string, object>>(Params);
            //var re = v.ToDictionary(p => p.Key, p => (p.Value as IConvertible));
          return v ;
        }     
        #endregion

        /// <summary>
        /// 历史记录
        /// </summary>
        public virtual ICollection<ApprovalRecord> Records { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 参数
        /// </summary>
        public string Params { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public virtual Employee Creater { get; set; }
        /// <summary>
        /// 当前审批项
        /// </summary>
        public virtual SequenceApprovalItem NowItem { get; set; }

        /// <summary>
        /// 审批是否重写
        /// </summary>
        public bool NowIsRedirect { get; set; }

        /// <summary>
        /// 当前审批人
        /// </summary>
        public virtual Employee NowApprover { get; set; }

        /// <summary>
        /// 当前审批人编号
        /// </summary>
        public Guid? NowApproverId { get; set; }

        /// <summary>
        /// 当前审批人名称
        /// </summary>
        public string NowApproverName { get; set; }

        /// <summary>
        /// 审批备注
        /// </summary>
        public string NowRemark { get; set; }

        /// <summary>
        /// 是否是负责人
        /// </summary>
        public bool IsAdminHandle { get; set; }

        /// <summary>
        /// 审批状态
        /// </summary>
        public ApprovalState ApprovalState { get; set; }

        /// <summary>
        /// 关联编号
        /// </summary>
        public string Tag { get; set; }
        public class Mapper : SalMapper<SequenceApproval, Guid>
        {
            public Mapper()
            {
                Property(p => p.NowRemark).HasMaxLength(4000);
                // HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
            }
        }

    }

    /// <summary>
    /// 审批节点
    /// </summary>
    public class SequenceApprovalItem : DbEntityBase<Guid>
    {

        //public virtual SequenceApproval Approval { get; set; }
        //public virtual Guid ApprovalId { get; set; }

        /// <summary>
        /// 类型名称
        /// </summary>
        public string TypeName { get; set; }

        /// <summary>
        /// 表达式
        /// </summary>
        public string ExeExpression { get; set; }

        /// <summary>
        /// 节点表达式
        /// </summary>
        public string ApproverExpression { get; set; }

        /// <summary>
        /// 创建人
        /// </summary>
        public virtual Employee Approver { get; set; }
        /// <summary>
        /// 审批人名称
        /// </summary>
        public string ApproverName { get; set; }

        /// <summary>
        /// 节点顺序
        /// </summary>
        public int Index { get; set; }
        /// <summary>
        /// 节点名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; set; }
        public class Mapper : SalMapper<SequenceApprovalItem, Guid>
        {
            public Mapper()
            {
                Property(p => p.Remark).HasMaxLength(255);
                // HasMany(p => p.Records).WithOptional().WillCascadeOnDelete();
            }
        }
    }

    /// <summary>
    /// 审批状态
    /// </summary>
    public enum ApprovalState
    {
        /// <summary>
        /// 等待
        /// </summary>
        [Description("等待审批")]
        Pending = 0,
        /// <summary>
        /// 审批中
        /// </summary>
        [Description("审批中")]
        Executing = 1,
        /// <summary>
        /// 通过
        /// </summary>
        [Description("通过")]
        Agree = 2,
        /// <summary>
        /// 未通过
        /// </summary>
        [Description("已驳回")]
        Disagree = 3

    }

    public enum FinaceApprovalState
    {
        /// <summary>
        /// 审批中
        /// </summary>
        [Description("审批中")]
        Executing = 0,

        /// <summary>
        /// 审批通过
        /// </summary>
        [Description("审批通过")]
        ApprovePass = 1,

        /// <summary>
        /// 审批驳回
        /// </summary>
        [Description("审批驳回")]
        ApproveRefuse = 2,

        /// <summary>
        /// 通过
        /// </summary>
        [Description("通过")]
        Agree = 3,

        /// <summary>
        /// 未通过
        /// </summary>
        [Description("未通过")]
        Disagree = 4
    }
    
    /// <summary>
    /// 支付状态
    /// </summary>
    public enum PaymentStatus
    {
        /// <summary>
        /// 付款
        /// </summary>
        [Description("付款")]
        Payment = 0,
        /// <summary>
        /// 退款
        /// </summary>
        [Description("退款")]
        Refund = 1
    }
    

    /// <summary>
    /// 审批记录
    /// </summary>
    public class ApprovalRecord : DbEntityBase<Guid>
    {
        public ApprovalRecord()
        {
            CreateTime= DateTime.Now;
        }
        public class Mapper : SalMapper<ApprovalRecord, Guid>
        {
            public Mapper()
            {

            }
        }

        /// <summary>
        /// 审批
        /// </summary>
        public virtual SequenceApproval ApprovalEntity { get; set; }

        /// <summary>
        /// 审批编号
        /// </summary>
        public virtual Guid ApprovalEntityId { get; set; }

        /// <summary>
        /// 审批人
        /// </summary>
        public virtual Employee Approver { get; set; }

        /// <summary>
        /// 是否通过
        /// </summary>
        public bool IsAgree { get; set; }

        /// <summary>
        /// 审批意见
        /// </summary>
        public string Opinion { get; set; }

        /// <summary>
        /// 审批节点
        /// </summary>
        public virtual SequenceApprovalItem ApprovalItem { get; set; }

        /// <summary>
        /// 审批节点编号
        /// </summary>
        public virtual Guid ? ApprovalItemId { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remarks { get; set; }
    }


}
